package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.entity.PageResult;
import com.itheima.mapper.MyUserMapper;
import com.itheima.pojo.User;
import com.itheima.service.MyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.List;

@Service(interfaceClass = MyUserService.class)
public class MyUserServiceImpl implements MyUserService {


    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Autowired
    private MyUserMapper myUserMapper;

    @Override
    public void add(Integer[] roleIds, User user) {
        //新增
        myUserMapper.add(user);
        //设置中间表
        setRoleAndUser(roleIds, user.getId());
    }

    /*
     * @Author: hyy
     * @Description:设置中间表结构
     * @Date: 22:12 2020/6/30
     * @param: null
     * @return:
     * @Version: 1.0
     */
    private void setRoleAndUser(Integer[] roleIds, Integer id) {
        if (roleIds != null && roleIds.length > 0) {
            for (Integer roleId : roleIds) {
                //循环设置中间表关系
                myUserMapper.setRoleAndUser(roleId, id);
            }
        }
    }

    /*
     * @Author: hyy
     * @Description:删除业务
     * @Date: 22:12 2020/6/30
     * @param: null
     * @return:
     * @Version: 1.0
     */
    @Override
    public void deleteById(Integer id) {
        //通过id删除用户和角色中的间接关系
        myUserMapper.deleteAssociation(id);
        //通过id删除用户
        myUserMapper.deleteById(id);
    }

    /*
     * @Author: hyy
     * @Description:通过分页插件查询
     * @Date: 22:12 2020/6/30
     * @param: null
     * @return:
     * @Version: 1.0
     */
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        //使用Mybatis的分页插件
        PageHelper.startPage(currentPage, pageSize);
        //条件查询分页数据
        List<User> userList = myUserMapper.findByCondition(queryString);
        //将查询出来的结果进行封装
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        PageResult pageResult = new PageResult(pageInfo.getTotal(), pageInfo.getList());
        return pageResult;
    }

    @Override
    public User findById(Integer id) {
        return myUserMapper.findById(id);
    }

    /*
     * @Author: hyy
     * @Description:编辑业务
     * @Date: 22:13 2020/6/30
     * @param: null
     * @return:
     * @Version: 1.0
     */
    @Override
    public void edit(Integer[] roleIds, User user) {
        //删除中间表关系
        myUserMapper.deleteAssociation(user.getId());
        //更新中间表表关系(调用之前的方法即可)
        setRoleAndUser(roleIds, user.getId());
        myUserMapper.edit(user);
    }

    @Override
    public User findUserByName(String username) {
        return myUserMapper.findUserByName(username);
    }

    @Override
    public List<Integer> findRoleIdsByUserId(Integer id) {
        return myUserMapper.findRoleIdsByUserId(id);
    }

    @Override
    public void updatePassword(String username, String password, String newPassword) {
        if (username != null && password != null && newPassword != null) {
            //先根据用户名查找密码
            User user = myUserMapper.selectPasswordByname(username);
            //判断当前的密码和原始密码一样吗
            if (user.getPassword().equals(password)) {
                //如果一样，就把新密码加密
                user.setPassword(passwordEncoder.encode(newPassword));
                myUserMapper.updatePassword(user);
            }
        }
    }

}
